import type { NextConfig } from "next";
import type { Configuration as WebpackConfig, RuleSetRule } from "webpack";
import path from 'path';

// 修改导入语法以匹配类型声明
import withLess = require('next-plugin-less');

interface WebpackConfigContext {
    buildId: string;
    dev: boolean;
    isServer: boolean;
    defaultLoaders: {
        babel: any;
    };
}

const nextConfig: NextConfig = {
    output: 'standalone', // 添加这行以支持独立部署
    reactStrictMode: true,
    transpilePackages: ["@nextui-org/react"], // 确保 NextUI 组件被正确编译
    images: {
        domains: ['img.shetu66.com', 'pic4.zhimg.com', 'pic3.zhimg.com'], // 添加允许的图片域名
    },
    async rewrites() {
        return [
            {
                source: '/api1/:path*',
                destination: `http://139.9.159.215:8999/:path*`,
            },
            {
                source: '/api2/:path*',
                destination: `http://124.222.25.229:8091/:path*`,
            },
            {
                source: '/api3/:path*',
                // destination: `http://192.168.3.149:8090/:path*`,
                destination: `http://124.222.25.229:8083/:path*`,
            },
        ];
    },

    webpack(config: WebpackConfig, options: WebpackConfigContext) {
        // 添加 less 文件处理规则
        config.module.rules.push({
            test: /\.less$/,
            use: [
                'style-loader',
                'css-loader',
                {
                    loader: 'less-loader',
                    options: {
                        lessOptions: {
                            javascriptEnabled: true,
                        },
                    },
                },
            ],
        });

        // 原有的规则处理
        config.module.rules.forEach((rule: RuleSetRule) => {
            const { oneOf } = rule as any;
            if (oneOf) {
                oneOf.forEach((one: any) => {
                    if (!`${one.issuer?.and}`.includes('_app')) return;
                    one.issuer.and = [path.resolve(__dirname)];
                });
            }
        });
        return config;
    }
};

// 使用 withLess 包装配置
export default withLess({
    ...nextConfig,
    eslint: {
        ignoreDuringBuilds: true, // 忽略 eslint 检查
    },
    typescript: {
        ignoreBuildErrors: true, // 忽略 TypeScript 检查
    },
    lessLoaderOptions: {
        lessOptions: {},
    },
});
